<?php if(isSet($this->user)){?>
    <form action="http://school.loc/message/add" method="post">
        <br>
        <textarea name="text" rows="2" cols="52" style="text-align: left"></textarea>
        <div style="margin-right: 65px; padding-bottom: 30px;">
            <div id="add_video" class="add_links" style="color: #AAA">Add video</div>
            <div id="add_tags" class="add_links" style="color: #AAA">Add tags</div>
        </div>
        <input type="text" name="video" placeholder="Add video link-url here" style="width: 430px; display: none;">
        <input type="text" name="tags" placeholder="Add some tags here" style="width: 430px; display: none">
        <div style="text-align: right; margin-right: 57px;">
            <input type="submit" value="Send">
        </div>
    </form>
    <br>
<?} else {?>
    <a href="user/login"><input type="button" value="Log in!"></a>
    <br>
    <br>
<?}?>
<?php
    foreach($this->posts as $post){?>
        <div class="message" style="overflow:hidden;text-align: left; padding: 10px; margin-left: 55px; margin-right: 55px; border-top-width: 1px; border-top-color: #DDD; border-top-style: solid;">
            <b><a href="/?author=<?=$post['author']->getId()?>">
                <?=$post['author']->getNick();?>
            </a></b>
            <sup style='color: #888; font-size: 11px;'>
                <?=$post['author']->getRating();?>
            </sup>
            <input type="hidden" title="author" value="<?=$post['author']->getId()?>">
            <?  if(!empty($this->user) && ($this->user->getId() != $post['author']->getId()))
                {?>
                <div class="star"><input type="hidden" value="<?=$post['author']->getId()?>"></div>
            <?  }?>
            <div style="margin-left: 20px;">
            <?
                echo $post['message']['text'];
                echo $post['message']['video'];
                if(!empty($post['tags']))
                {
                    echo "<div style='font-size: 11px; color: #888'>";
                    foreach($post['tags'] as $tag)
                    {
                        echo "<a href='/?tag=" . $tag['id'] . "'>" . $tag['tag'] . "</a> ";
                    }
                    echo "</div>";
                }

                if(isset($this->user) && ($post['author']->getId() == $this->user->getId()))
                {
                    ?>
                    <div style="float: right; font-size: 12px; margin: 5px; color:#888">
                        <a style="text-align: right; color: #AAA"  href="/message/edit?id=<?=$post['message']['id']?>">Edit</a>
                    </div>
                    <div style="float: right; font-size: 12px; margin: 5px;">
                        <a style="text-align: right; color: #AAA;" href="/message/delete?id=<?=$post['message']['id']?>">Delete</a>
                    </div>
                    <?
                }

                if(!empty($post['comments']))
                {
                    echo "<br><br>";
                    foreach($post['comments'] as $comment)
                    {
                        ?>
                            <div style=" padding: 5px; margin-right: 5px; font-size: 13px; background-color: #F1F1F1; ;width: 400px; float: right; border-top-width: 1px; border-top-color: #E8E8E8; border-top-style: solid;">
                                <b><?=$comment['author']->getNick()?></b><br>
                                <div style="margin-left: 20px; font-size: 14px;">
                                    <?=$comment['text']?>
                                </div>
                            </div>
                        <?
                    }
                }
                if(isSet($this->user))
                {
                    ?>
                    <div class="comment_panel">
                        <div class="add_comment" style="color: #AAA">Add comment</div>
                        <form style="text-align:left; display: none; margin: 0px; color:#888;" action="/comment/add" method="post">
                            <input type="hidden" value="<?=$post['message']['id']?>" name="message">
                            <input type="hidden" value="<?=$this->user->getId()?>" name="author">
                            <input type="text" name="text" placeholder="Add comment here" style="width: 400px">
                            <input type="submit" value="Send">
                        </form>
                    </div>
                    <?
                }
            ?>
            </div>
        </div>
    <?}
?>
<script>

    $('.star').click(function()
    {
        var OK = false;
        $.ajax({
            type: "POST",
            async: false,
            url: "/ajax/uprating",
            data: "user=" + $(this).find("input[type=hidden]").val(),
            success: function(html)
            {
                switch (html)
                {
                    case "OK":
                    {
                        OK = true;
                        break;
                    }
                    case "TIME":
                    {
                        OK = false;
                        break;
                    }
                    default:
                    {
                        alert("error - " + html);
                        break;
                    }
                }
            }
        });
        if(OK) $(this).css({'opacity':'1'});
    });

    $('.comment_panel').find('form').hide();

    $('.add_comment').click(function(){
        $('.add_comment').next('form').hide(); //скрываем последний открытый
        $('.add_comment').show();              //показываем все кнопки которые были закрыты
        var form = $(this).next('form');
        $(this).hide();
        form.show('blind', 100, function(){
            form.find('input[type=text]').focus();
        });
    });

    $('#add_video').click(function(){
        $('input[name=video]').show('blind',100);
        $(this).hide('drop',{'direction':'down'},500);
    });
    $('#add_tags').click(function(){
        $('input[name=tags]').show('blind',100);
        $(this).hide('drop',{'direction':'down'},500);
    });

    $('textarea[name=text]').focusin(function()
    {
        $(this).attr({'rows':'4'});
    });

    function ajaxLoadRating()
    {
        var users = getUsers();
        var copyUsers = [].concat(users);
        ajaxSend('ajax/getallratings', serializeArray(copyUsers, 'users'), onGetAllRatings);

        function onGetAllRatings(xhr, xhrStatus)
        {
            if(xhrStatus == 'success')
            {
                var ratings = xhr.responseText.split(', ');
                setRatings(users, ratings);
            }
        }
    }
    setInterval(ajaxLoadRating,3000);
</script>